<?php

namespace app\admin\controller;

use app\api\controller\Facebook;
use app\common\controller\Backend;

use think\Cache;
use think\Db;

use fast\Tree;
/**
 * facebook广告ROI
 *
 * @icon fa fa-circle-o
 */
class FeditionAdvertisingRoi extends Backend
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    /**
     * FeditionAdvertisingRoi模型对象
     * @var \app\admin\model\FeditionAdvertisingRoi
     */
    protected $model = null;
    protected $rulelist = null;
    protected $cache_info = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\FeditionAdvertisingRoi;
        $this->view->assign("statusList", $this->model->getStatusList());

        // 必须将结果集转换为数组
        $this->rulelist = Cache::get('rulelist');
        if (empty($this->rulelist)) {
            $ruleList = collection($this->model->order('createtime DESC')->select())->toArray();
            foreach ($ruleList as $k => &$v) {
                $v['name'] = __($v['name']);
            }
            Tree::instance()->init($ruleList);
            $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
            $ruledata = [0 => __('None')];
            foreach ($this->rulelist as $k => &$v) {
                $ruledata[$v['id']] = $v['name'];
                $v['name'] = str_replace(['&nbsp',';',' '],'',$v['name']);
            }
            Cache::set('rulelist',$this->rulelist,3600);
            $this->cache_info = 1;
        }else{
            $this->cache_info = 0;
        }
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $list = $this->rulelist;
            $total = count($this->rulelist);
            foreach ($list as $k=>$v) {
                $accounturl = Db::name('fedition_accounturl')->where('id', '=', $v['accounturl'])->find();
                if($accounturl){
                    $list[$k]['accounturl_name'] = $accounturl['name'];
                }else{
                    $list[$k]['accounturl_name'] = '店铺名称已删除';
                }

                $advertisin = Db::name('fedition_advertising')->where('id', '=', $v['advertising_id'])->find();
                if($advertisin){
                    $list[$k]['advertising_name'] = $advertisin['info'];
                }else{
                    $list[$k]['advertising_name'] = '';
                }
            }
            $result = array("total" => $total, "rows" => $list, "cache_info" => $this->cache_info);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 修改状态
     */
    public function edit_status()
    {
        $id = $this->request->param('ids');
        if(!empty($id)){
            if(!empty($id)){
                //        更新每日汇总
                $Facebook = new Facebook();
                $return = $Facebook->updatetime($id);
                $this->success('更新成功','', $return);
            }else{
                $this->error('参数错误，请重新登录');
            }
        }else{
            $this->error('参数错误，请重新登录');
        }
    }

}
